package com.mallcai.bigdata.ladon.datasource;

import com.mallcai.bigdata.ladon.utils.ExceptionUtils;
import lombok.Data;

import java.io.Serializable;

/**
 * @author feixy
 * @version 1.0
 * @date 2019-07-20 09:13
 */
@Data
public class RunInfoResult<T> implements Serializable {

    private static final long serialVersionUID = -4710563648170825246L;
    private T data;

    private Exception error;

    private String sql;

    private int timeElapsed; // 毫秒

    public RunInfoResult(T data, Exception error, String sql, int timeElapsed) {
        this.data = data;
        this.error = error;
        this.sql = sql;
        this.timeElapsed = timeElapsed;
    }

    public boolean isSuccess(){
        return error == null;
    }

    public String getErrorInfo(){
        if (error != null){
            return ExceptionUtils.buildErrorMessage(error);
        }
        return null;
    }


    public static <T> RunInfoResult<T> createEmptyResult(T data){
        return new RunInfoResult<>(data, null, "", 0);
    }
}
